#include "mycoin.h"
#include <QDebug>
MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if(!ret)
    {
        qDebug()<<"图片加载失败";
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:none}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));
    this->timer1 = new QTimer(this);
    this->timer2 = new QTimer(this);
    connect(this->timer1,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str = QString(":/res/Coin000%1").arg(this->min++);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:none}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        //如果翻完了将min重置为1
        if(this->min>this->max)
        {
            this->min=1;
            this->timer1->stop();
            this->isAnimation=false;
        }
    });
    connect(this->timer2,&QTimer::timeout,[=](){
        QPixmap pix;
        QString str = QString(":/res/Coin000%1").arg(this->max--);
        pix.load(str);
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:none}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
        //如果翻完了将min重置为1
        if(this->max<this->min)
        {
            this->max=8;
            this->timer2->stop();
            this->isAnimation=false;
        }
    });
}
void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->isAnimation||this->isWin)
    {
        return;
    }
    QPushButton::mousePressEvent(e);
}
//改变正反面标志的方法
void MyCoin::changeFlag()
{
    //如果是正面翻成反面
    if(this->flag)
    {
        this->flag=false;
        //开启正面到反面的定时器
        this->timer1->start(30);

    }else{
        this->flag=true;
        //开启正面到反面的定时器
        this->timer2->start(30);
    }
    this->isAnimation=true;

}
